package open.api.strategy;

/**
 * ClassName: PayService
 * Package: open.api.strategy
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2024/5/5 - 16:17
 * @Version: v1.0
 */

import com.alibaba.nacos.shaded.com.google.protobuf.ServiceException;
import open.api.exception.SystemException;
import open.api.model.entity.Pay;
import open.api.response.ResponseCodeEnum;
import open.api.response.ResultData;
import open.api.strategy.impl.AlipayStrategyImpl;
import open.api.strategy.impl.WechatPayStrategyImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建支付服务类，根据用户选择的支付方式创建对应的策略对象并执行支付操作：
 */
@Service("payService")
public class PayService {

    private Map<String, PayStrategy> payStrategyMap = new HashMap<String, PayStrategy>();

    public PayService() {
        // 将支付宝和微信支付策略对象放入Map中
        payStrategyMap.put("alipay",new AlipayStrategyImpl());
        payStrategyMap.put("wxpay",new WechatPayStrategyImpl());
    }

    public ResultData pay(Pay pay) throws SystemException {
        // 根据用户选择的支付方式获取对应的策略对象
        PayStrategy payStrategy = payStrategyMap.get(pay.getPaymentType());
        if (payStrategy == null) {
            throw new SystemException(ResponseCodeEnum.PEY_TYPE_UNSUPORT);
        }
        // 执行支付操作
        return payStrategy.pay(pay);
    }
}
